package cn.bytecollege.controller;


import cn.bytecollege.constant.Resource;
import cn.bytecollege.entity.Vip;
import cn.bytecollege.service.VipService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * 添加用户控制器
 * @author Mr.East
 */
@WebServlet(name = "VipAddController",value = "/vipAdd")
public class VipAddController extends HttpServlet {
    String msg=null;
    private VipService vipService = null;
    public VipAddController(){
        vipService = new VipService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     request.getRequestDispatcher(Resource.PAGE_LOCATION+"vipAdd.jsp").forward(request,response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接受请求参数
//        String vipId = request.getParameter("vipId");
        String vipName = request.getParameter("vipName");
        String vipGender = request.getParameter("vipGender");
        String vipIdNumber = request.getParameter("vipIdNumber");
        String vipPhoneNumber = request.getParameter("vipPhoneNumber");
        String vipCreateTime = request.getParameter("vipCreateTime");
        Vip vip1=vipService.findByIdNumber(vipIdNumber);
        //封装数据
        if (vip1!=null){
            msg="该vip信息已存在";
            request.setAttribute("msg",msg);
            request.getRequestDispatcher(Resource.PAGE_LOCATION+"/vipAdd.jsp").forward(request,response);
            return;
        }
        Vip vip = new Vip();
//        vip.setVipId(Integer.valueOf(vipId));
        vip.setVipName(vipName);
        vip.setVipGender(vipGender);
        vip.setVipIdNumber(vipIdNumber);
        vip.setVipPhoneNumber(vipPhoneNumber);
        vip.setVipCreateTime(LocalDateTime.now());
        //调用Service方法
        vipService.save(vip);
        //跳转页面
        response.sendRedirect(request.getContextPath()+"/vipList");
    }
}
